/*
**NCS provide dev head-files for the dev objects
**
**This module provide the objects of dev(pp,sig,rx,rcst) 
**
**Copyright (C) 2016-1 andisat <www.andisat.com>
**
**Author: andisat
**Date:2016-1
**
*/

#ifndef __NCS_DEV__
#define __NCS_DEV__

//#include "ncs_cfg.h"
#include "ncs_list.h"


#define DEV_NAME_MAXLEN  65
#define DEV_SN_MAXLEN  9
#define DEV_ABILITY_MAXLEN  32

#define DEF_POLLING_INTERVAL 10
#define DEF_POLLING_GROUPNUM 10

#define DEV_FLAG_ACTIVE 0X00000100
#define DEV_FLAG_LOCK 0X00000200
#define DEV_FLAG_BOD 0X00000400
#define DEV_FLAG_BODREL 0X00000800

#define CHECK_BIT(x,y) ((x) & (y))
#define SET_BIT(x,y) ((x)|(y))
#define UNSET_BIT(x,y) ((x) & (~y))

enum RESULT{
    SUCCESS=0,
    FAILURE
};

enum ENUM_CHANNLE_TYPE{
    TXCHA_TYPE=1,
    RXCHA_TYPE
};

enum ENUM_DEV_PATH_NUM{
    PATH_FIRST=1,
    PATH_SECOND,
    PATH_THIRD,
    PATH_FOURTH
};

enum DEV_TYPE_DEFINE{
    DEV_TYPE_NULL=0,
    DEV_TYPE_NMS,
    DEV_TYPE_NCS,
    DEV_TYPE_PP,
    DEV_TYPE_RXEMS,
    DEV_TYPE_SIGEMS,
    DEV_TYPE_RCST,
    DEV_TYPE_MAX
};

#pragma pack(1)
struct dev_obj{
    char type;
    int devId;            
    char name[DEV_NAME_MAXLEN];          
    char devSN[DEV_SN_MAXLEN];         
    //char type[DEV_ABILITY_MAXLEN];
    
    int ip;                
    int port;                 

    int state;  
    int flag;            
    unsigned int pollTries;
    unsigned int pollSeq;
    int s_seq;
    int r_seq;
    int try_flag;
    
    struct list *out;     
};

struct dev_set{
    int sum;                  
    int count;           /*online count*/   
    void (*poll)(int fd,short event,void *arg);                
    struct event *poll_timerev;    
    struct list *dev_list;          
};

struct ncs_object{
    struct event_base *g_base;
    
    int servd_fd;
    int servd_ip;
    int client_fd;
    short servd_port;

    int state;
    int devId;

    int g_retransmit_max;
    int g_retransmit_timeout;

    struct event *ncs_serv_recvev;
    struct event *ncs_client_recvev;
    struct event *send_ib_timerev;
    struct event *send_broadcast_timerev;

    struct dev_set *nms_set;
    struct dev_set *pp_set;
    struct dev_set *sig_set;
    struct dev_set *rx_set;
    struct dev_set *rcst_set;
    
    void (*ncs_serv_recv)(int fd,short event,void *arg);     /*recv PP RXEMS RCST SIGEMS packets*/
    void (*ncs_client_recv)(int fd,short event,void *arg);    /*recv ncs client process's message*/
    void (*ncs_send_IBinfo)(int fd,short event,void *arg);  /*send IB information to SIGEMS*/
};
#pragma pack()

/*declare functions for ncs_dev.c*/
void free_ncs(struct ncs_object *ncs);
void init_ncs(struct ncs_object *ncs);
void ncs_del_pp(struct ncs_object *ncs,struct dev_obj *pp);
void ncs_add_dev(struct list *dev_list,struct dev_obj *dev);
void ncs_del_modem(struct ncs_object *ncs,struct dev_obj *sig);
int rx_rxpath_used_add(struct dev_obj *rx,char num);
int rx_if_used(struct dev_obj *rx);
int rx_rxpath_used_dec(struct dev_obj *rx,char num);
struct dev_obj *build_dev();
struct dev_obj *ncs_find_pp(struct ncs_object *ncs,int devId);
struct dev_obj *ncs_find_sig(struct ncs_object *ncs,int devId);
struct dev_obj *ncs_find_rx(struct ncs_object *ncs,int devId);
struct dev_obj *ncs_find_rcst(struct ncs_object *ncs,int devId);
struct dev_obj *ncs_get_active_sig(struct ncs_object *ncs);
int dev_if_online(struct dev_obj *dev);

#endif
